package com.it;

import java.io.*;

public class FileCopy {
    /**
     * 字节流可以复制一切文件
     * 先打开的流后关闭
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        try (//创建输入流
             InputStream fin = new FileInputStream("C:\\Users\\张国靖\\Desktop\\demo\\src\\main\\java\\com\\it\\text1");
             //创建输出流
             OutputStream fOut = new FileOutputStream("C:\\Users\\张国靖\\Desktop\\demo\\src\\main\\java\\com\\it\\text2");
        ) {
            //读取文件内容
            //byte[] buffer = fin.readAllBytes();  一次全部读出
            byte[] buffer = new byte[1024]; //一次读1024个字节
            //写入文件
            //fOut.write(buffer);
            int len;
            while ((len = fin.read(buffer)) != -1) {
                fOut.write(buffer, 0, len);
            }
            System.out.println("文件复制成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
